#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
typedef struct temp
{
    char ch[32];
    int true;
}temp,*ftemp;


int main(int argc,const char*  argv[]){

    key_t key = ftok("./app",'a');
    if(key < 0){
        perror("ftok err");
        return -1;
    }
    printf("%#x\n",key);

    int shmid = shmget(key,sizeof(temp),IPC_CREAT | IPC_EXCL | 0666);
    if(shmid <= 0){
        if(errno == EEXIST){
            shmid = shmget(key,sizeof(temp),0666);
        }else{
            perror("shmget err");
            return -1;
        }
    }

    printf("shmid:%d\n",shmid);

    ftemp fone=(void*)shmat(shmid,NULL,0);
    if(fone == (ftemp)-1){
        perror("shmat err");
        shmctl(shmid,IPC_RMID,NULL);//删除共享内存
        return -1;
    }

    memset(fone,0,sizeof(temp));

    while(1){
            
        if(fone->true == 1){//检测是否输入
            if(strcmp(fone->ch,"quit") == 0){
                break;
            }

            printf("输出:%s\n",fone->ch);
            fone->true=0;//归零(输出执行完的信号)
        }
    }

    shmdt(fone);//取消映射

    return 0;
}